package com.db.bankapp.domain;

import com.db.bankapp.exception.NotEnoughFundsException;

/**
 * @Not-ThreadSafe
 * Base implementation of @link{com.db.bankapp.domain.bank.Account}
 */
abstract class AccountBase implements Account {
    protected int balance;

    @Override
    public int getBalance() {
        return balance;
    }

    @Override
    public void deposit(int amount) {
        this.balance += amount;
    }

    @Override
    public void withdraw(int amount) throws NotEnoughFundsException {
        if (maximumAmountToWithdraw()<amount) {
            throw new NotEnoughFundsException("Not enough funds!", amount);
        }
        balance -= amount;
    }

    @Override
    public int maximumAmountToWithdraw() {
        return balance;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof AccountBase )) return false;
        if (o instanceof AccountBase) {
            return this.balance==((AccountBase)o).balance;
        }
        return false;
    }
    @Override
    public int hashCode() {
        int result=17;
        return result*31 + this.balance;
    }
    @Override
    public String toString() {
        Integer i = balance;
        return "balance " + i.toString()+"$";
    }
}

